package cn.com.inossem.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.inossem.base.RequestActivity;
import cn.com.inossem.config.EventCode;
import cn.com.inossem.http.Api;
import cn.com.inossem.http.NetworkConnectGet;
import cn.com.inossem.model.EventBean;
import cn.com.inossem.utils.MyDatePicker.MyDatePicker;
import cn.com.inossem.utils.MyDatePicker.picker.DatePicker;
import cn.com.inossem.utils.MyDatePicker.picker.DateTimePicker;
import cn.com.inossem.utils.ToastUtil;

/**
 * 我的TE
 * Created by Mickey.Li on 2017-8-9.
 */
public class MyTEActivity extends RequestActivity {
	public static final int TYPE_TE_HOUR = 1;//工时
	public static final int TYPE_TE_MONEY = 2;//报销
	private static String TE_HOUR_HTML = "<font><big><big> 0 </big></big></font>小时";
	private static String TE_EXP_HTML = "RMB<font><big><big> 0 </big></big></font>元";
	public static int TE_YEAR, TE_MONTH;
	@BindView(R.id.common_title)
	TextView commonTitle;
	@BindView(R.id.common_right_tv)
	TextView commonRightTv;
	@BindView(R.id.te_tab_hour_tv)
	TextView teTabHourTv;
	@BindView(R.id.te_tab_money_tv)
	TextView teTabMoneyTv;
	@BindView(R.id.te_vp)
	ViewPager teViewPager;
	@BindView(R.id.te_tab_hour_line)
	View teTabHourLine;
	@BindView(R.id.te_tab_money_line)
	View teTabMoneyLine;
	private List<Fragment> fragList;
	private JSONObject jb;
	private TeApplyListFragment fragHourList, fragExpList;
	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			try {
				dismissLoadPd();
				jb = new JSONObject((String) msg.obj);
				switch (msg.what) {
					case 1:
						jb = jb.getJSONObject("result");
						initTab(jb.getInt("teTotal"), jb.getDouble("reimburseTotal"));
						break;
					case 99:
						ToastUtil.show(MyTEActivity.this, (String) msg.obj);
						break;
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
	};
	private Calendar cal;

	@Override
	public int getContentViewId() {
		return R.layout.act_my_te;
	}

	@Override
	public void initAllMembers(Bundle savedInstanceState) {
		super.initAllMembers(savedInstanceState);
		EventBus.getDefault().register(this);
		commonTitle.setText(getString(R.string.te_title));
		findViewById(R.id.common_right_img).setVisibility(View.GONE);
		commonRightTv.setVisibility(View.VISIBLE);
		initTab(0, 0);
		cal = Calendar.getInstance();
		TE_YEAR = cal.get(Calendar.YEAR);
		TE_MONTH = cal.get(Calendar.MONTH) + 1;
		commonRightTv.setText(TE_YEAR + "年" + TE_MONTH + "月");
		initViewPager();
		requestTESum();
	}

	private void initTab(int hourT, double expenseT) {
		int measureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
		teTabHourTv.setText(Html.fromHtml(TE_HOUR_HTML.replace("0", "" + hourT)));
		teTabHourTv.measure(measureSpec, measureSpec);
		ViewGroup.LayoutParams layoutParams = teTabHourLine.getLayoutParams();
		layoutParams.width = teTabHourTv.getMeasuredWidth();
		teTabHourLine.setLayoutParams(layoutParams);
		teTabMoneyTv.setText(Html.fromHtml(TE_EXP_HTML.replace("0", "" + expenseT)));
		teTabMoneyTv.measure(measureSpec, measureSpec);
		layoutParams = teTabMoneyLine.getLayoutParams();
		layoutParams.width = teTabMoneyTv.getMeasuredWidth();
		teTabMoneyLine.setLayoutParams(layoutParams);
	}

	private void initViewPager() {
		fragList = new ArrayList<>();
		fragList.add(fragHourList = TeApplyListFragment.newInstance(TYPE_TE_HOUR));
		fragList.add(fragExpList = TeApplyListFragment.newInstance(TYPE_TE_MONEY));
		teViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

			}

			@Override
			public void onPageSelected(int position) {
				selectFrag(position);
			}

			@Override
			public void onPageScrollStateChanged(int state) {

			}
		});
		teViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
			@Override
			public Fragment getItem(int position) {
				return fragList.get(position);
			}

			@Override
			public int getCount() {
				return fragList.size();
			}
		});
	}

	/**
	 * 顶部tab选择器
	 */
	private void selectFrag(int position) {
		teTabHourTv.setTextColor(position == 0 ? getResources().getColor(R.color.app_main) : getResources().getColor(R.color.project_content));
		teTabHourLine.setVisibility(position == 0 ? View.VISIBLE : View.INVISIBLE);
		teTabMoneyTv.setTextColor(position == 1 ? getResources().getColor(R.color.app_main) : getResources().getColor(R.color.project_content));
		teTabMoneyLine.setVisibility(position == 1 ? View.VISIBLE : View.INVISIBLE);
	}

	/**
	 * 选中年月并更新数据
	 */
	public void notifyDate() {
		showLoadPd();
		commonRightTv.setText(TE_YEAR + "年" + TE_MONTH + "月");
		requestTESum();
		fragHourList.requestData();
		fragExpList.requestData();
	}

	private void requestTESum() {
		new NetworkConnectGet(this, Api.PROJECT_APPLY_TOTAL + "?year=" + TE_YEAR + "&month=" + TE_MONTH, mHandler, 1);
	}

	@OnClick({R.id.common_back, R.id.common_right_tv, R.id.te_tab_hour, R.id.te_tab_money})
	public void onViewClicked(View view) {
		switch (view.getId()) {
			case R.id.common_back:
				finish();
				break;
			case R.id.common_right_tv:
				showDatePick();
				break;
			case R.id.te_tab_hour:
				teViewPager.setCurrentItem(0);
				break;
			case R.id.te_tab_money:
				teViewPager.setCurrentItem(1);
				break;
		}
	}

	/**
	 * 年月选择器18
	 */
	private void showDatePick() {
		MyDatePicker picker = new MyDatePicker(this, DateTimePicker.YEAR_MONTH);
		picker.setRangeEnd(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1);
		picker.setRangeStart(2016, 1);
		picker.setSelectedItem(TE_YEAR, TE_MONTH);
		picker.setOnDatePickListener((DatePicker.OnYearMonthPickListener) (year, month) -> {
			TE_YEAR = Integer.parseInt(year);
			TE_MONTH = Integer.parseInt(month);
			notifyDate();
		});
		picker.show();
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void onEvent(EventBean bean) {
		if (bean.eventCode == EventCode.APPLY_HOUR_SUCC) {
			fragHourList.onClearData();
		} else if (bean.eventCode == EventCode.APPLY_EXPENSE_SUCC) {
			fragExpList.onClearData();
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		EventBus.getDefault().unregister(this);
	}
}